/*
 *  TestGazetteer.java
 *
 */

package edu.columbia.gate.plugins;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.regex.Pattern;

//import com.sun.xml.internal.ws.util.QNameMap;


import junit.framework.*;

import gate.*;
import gate.corpora.DocumentContentImpl;
import gate.corpora.TestDocument;
import gate.util.GateRuntimeException;

public class TestLineBreakNormalizer extends TestCase
{

	private static final boolean DEBUG = true;

	public TestLineBreakNormalizer(String name)
	{
		super(name);
	}

	/** Fixture set up */
	public void setUp() throws Exception
	{
		//make sure the right plugin is loaded

		Gate.init();
		
		File pluginsHome = new File(System
				.getProperty(GateConstants.GATE_HOME_PROPERTY_NAME), "plugins");

		try
		{
			URL pluginsUrl = new File(pluginsHome, "Tools").toURI().toURL();
			URL pluginsUrl2 = new File(
					"C:\\SVN\\StrayVoltage\\GATE\\Plugins\\",
					"GatePlugins").toURI().toURL();

			Gate.getCreoleRegister().registerDirectories(pluginsUrl2);
		} catch (Exception e)
		{
			throw new GateRuntimeException(e);
		}
	}

	public void tearDown() throws Exception
	{
	} // tearDown

	/** Test the default tokeniser */
	public void testEditDistanceGazetteer() throws Exception
	{
		//get a document
		Document doc = Factory
				.newDocument(new URL(
						"file:///C:/SVN/StrayVoltage/GATE/ProcessingResources/SampleTicketTest.xml"));

		//create a default gazetteer
		LineBreakNormalizer gaz = (LineBreakNormalizer) Factory
				.createResource("edu.columbia.gate.plugins.LineBreakNormalizer");
		
		//runtime stuff
		gaz.setDocument(doc);
		// gaz.setAnnotationSetName("GazetteerAS");
		// gaz.setListsURL(new File("../../ProcessingResources/TestStreetNamesGazetteer/", "StreetNamesGazetteer.def").toURI().toURL());

		gaz.execute();
		AnnotationSet resultAS = doc.getAnnotations("GazetteerAS");
		Annotation[] annots = new Annotation[resultAS.size()];
		resultAS.toArray(annots);
		for(int i=0; i<annots.length; i++)
		{
			SimpleAnnotation annot = (SimpleAnnotation) annots[i];
			long textStart = annot.getStartNode().getOffset().intValue();
			long textEnd = annot.getEndNode().getOffset().intValue();
			String normalString = ((String) annot.getFeatures().get("entryMatched"));
			DocumentContent normalDocCnt = new DocumentContentImpl(normalString);			
			gaz.getDocument().edit(textStart, textEnd, normalDocCnt);
		}
		System.out.println(gaz.getDocument().getContent().toString());
		
		assertTrue("Found " + resultAS.size()
				+ " annotations instead of the expected 53!",
				resultAS.size() == 53);
		resultAS.clear();

		resultAS.clear();
		Factory.deleteResource(gaz);
		Factory.deleteResource(doc);
	}

	private class AnnotationComparator implements Comparator<Annotation>
	{
		@Override
		public int compare(Annotation o1, Annotation o2)
		{
			int textStart1 = ((SimpleAnnotation) o1).getStartNode().getOffset().intValue();
			int textStart2 = ((SimpleAnnotation) o2).getStartNode().getOffset().intValue();
			if(textStart1 > textStart2)
				return 1;
			else if(textStart1 < textStart2)
				return -1;
			else
				return 0;
//			int textLength1 = ((String) ((SimpleAnnotation) o1).getFeatures().get("entryMatched")).length();
//			int textLength2 = ((String) ((SimpleAnnotation) o2).getFeatures().get("entryMatched")).length();
//			if(textLength1 < textLength2)
//				return 1;
//			else if(textLength1 > textLength2)
//				return -1;
//			else
//				return 0;
		}
		
	}
	/** Test suite routine for the test runner */
	public static Test suite()
	{
		return new TestSuite(TestLineBreakNormalizer.class);
	} // suite

	public static void main(String[] args)
	{
		try
		{
			Gate.init();
			TestLineBreakNormalizer testGaz = new TestLineBreakNormalizer(
					"");
			testGaz.setUp();
			testGaz.testEditDistanceGazetteer();
			testGaz.tearDown();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	} // main

} // TestEditDistanceGazetteer
